import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import BlogManagement from '@/views/list/BlogManagement.vue'
import BlogView from '@/views/list/BlogView.vue'
import BlogList from '@/views/list/BlogList.vue'
import Login from '@/views/Login.vue'
import Layout from '@/views/Layout.vue'
import ClassManagement from '@/views/list/ClassManagement.vue'
import UserManagement from '@/views/list/UserManagement.vue'
import Statistics from '@/views/list/Statistics.vue'
import MyCollections from '@/views/list/MyCollections.vue'
import Profile from '@/views/Profile.vue'
import index from '@/views/index.vue'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    redirect: '/blogview',
    component: Layout,
    meta: { requiresAuth: true },
    children: [
      {
        path:'index',
        name: 'index',
        component: index,
        meta: { requiresAuth: true }
      },
      {
        path: '/blogmanagement',
        name: 'blogmanagement',
        component: BlogManagement,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: '/blogview',
        name: 'blogview',
        component: BlogList,
        meta: { requiresAuth: true }
      },
      {
        path: '/blog/:id',
        name: 'blog-detail',
        component: BlogView,
        meta: { requiresAuth: true }
      },
      {
        path: '/collections',
        name: 'collections',
        component: MyCollections,
        meta: { requiresAuth: true }
      },
      {
        path: '/class',
        name: 'class',
        component: ClassManagement,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: '/user',
        name: 'user',
        component: UserManagement,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: '/statistics',
        name: 'statistics',
        component: Statistics,
        meta: { requiresAuth: true }
      },
      {
        path: '/profile',
        name: 'profile',
        component: Profile,
        meta: { requiresAuth: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()

  if (to.meta.requiresAuth === false) {
    if (userStore.isLoggedIn && to.name === 'login') {
      if (userStore.isAdmin()) {
        next('/blogmanagement')
      } else {
        next('/blogview')
      }
      return
    }
    next()
    return
  }

  if (!userStore.isLoggedIn) {
    const isLoggedIn = await userStore.checkLoginStatus()
    if (!isLoggedIn) {
      next('/login')
      return
    }
  }

  if (to.meta.requiresAdmin && !userStore.isAdmin()) {
    ElMessage.error('权限不足，无法访问该页面')
    next('/blogview')
    return
  }

  next()
})

export default router